PageRenderTime 71ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/inst/cPanel.php

https://github.com/DarrenH/takeaway_repo
PHP | 1176 lines | 633 code | 73 blank | 470 comment | 53 complexity | 16f210caa007edd277fa26f08ba701f8 MD5 | raw file
  1. <?php
  2. /*
  3. **********************************************
  4. **********************************************
  5. ***PHP cPanel API ***
  6. ***Copyright Brendan Donahue, 2006 ***
  7. **********************************************
  8. ***Feature List: ***
  9. *** Connect to cPanel via HTTP or SSL ***
  10. *** List bandwidth and disk space usage ***
  11. *** Change contact settings/passwords ***
  12. *** List, create, modify, and delete: ***
  13. *** Databases and MySQL users ***
  14. *** FTP and email accounts, quotas ***
  15. *** Parked, addon, and subdomains ***
  16. *** Apache redirects ***
  17. *** Email autoresponders ***
  18. *** Forwarders and default addresses***
  19. **********************************************
  20. **********************************************
  21. */
  22. /**
  23. * @ignore
  24. */
  25. class HTTP
  26. {
  27. function HTTP($host, $username, $password, $port = 2082, $ssl = '', $theme = 'x')
  28. {
  29. $this->ssl = $ssl ? 'ssl://' : '';
  30. $this->username = $username;
  31. $this->password = $password;
  32. $this->theme = $theme;
  33. $this->auth = base64_encode($username . ':' . $password);
  34. $this->port = $port;
  35. $this->host = $host;
  36. $this->path = '/frontend/' . $theme . '/';
  37. }
  38. function getData($url, $data = '',$stop = false)
  39. {
  40. $url = $this->path . $url;
  41. if(is_array($data))
  42. {
  43. $url = $url . '?';
  44. foreach($data as $key=>$value)
  45. {
  46. $url .= urlencode($key) . '=' . urlencode($value) . '&';
  47. }
  48. $url = substr($url, 0, -1);
  49. }
  50. if($stop === true)
  51. {
  52. print_r($url);
  53. die();
  54. }
  55. $response = '';
  56. $fp = fsockopen($this->ssl . $this->host, $this->port);
  57. if(!$fp)
  58. {
  59. return false;
  60. }
  61. $out = 'GET ' . $url . ' HTTP/1.0' . "\r\n";
  62. $out .= 'Authorization: Basic ' . $this->auth . "\r\n";
  63. $out .= 'Connection: Close' . "\r\n\r\n";
  64. fwrite($fp, $out);
  65. while (!feof($fp))
  66. {
  67. $response .= @fgets($fp);
  68. }
  69. fclose($fp);
  70. //print_r($response);
  71. return $response;
  72. }
  73. }
  74. /**
  75. * Functions to manipulate cPanel
  76. */
  77. class cPanel
  78. {
  79. /**
  80. * Creates an object to manipulate cPanel
  81. * @param string $host cPanel host without leading http://
  82. * @param string $username cPanel username
  83. * @param string $password cPanel password
  84. * @param int $port cPanel port, default to 2082. Change to 2083 if using SSL
  85. * @param bool $ssl False for http (default), true for SSL (requires OpenSSL)
  86. * @param string $theme cPanel theme, (forward compatibility- 'x' theme currently required)
  87. * @return cPanel
  88. */
  89. function cPanel($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x')
  90. {
  91. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  92. }
  93. /**
  94. * Change cPanel's password
  95. *
  96. * Returns true on success or false on failure.
  97. * The cPanel object is no longer usable after changing the password.
  98. * @param string $password new password
  99. * @return bool
  100. */
  101. function setPassword($password)
  102. {
  103. $data['oldpass'] = $this->HTTP->password;
  104. $data['newpass'] = $password;
  105. $response = $this->HTTP->getData('passwd/changepass.html', $data);
  106. if(strpos($response, 'has been') && !strpos($response, 'could not'))
  107. {
  108. return true;
  109. }
  110. return false;
  111. }
  112. /**
  113. * Retrieve contact email address.
  114. *
  115. * Returns the contact email address listed in cPanel.
  116. * @return string
  117. */
  118. function getContactEmail()
  119. {
  120. $email = array();
  121. preg_match('/email" value="(.*)"/', $this->HTTP->getData('contact/index.html'), $email);
  122. return $email[1];
  123. }
  124. /**
  125. * Modify contact email address
  126. *
  127. * Returns true on success or false on failure.
  128. * @param string new contact email address
  129. * @return string
  130. */
  131. function setContactEmail($email)
  132. {
  133. $data['email'] = $email;
  134. $response = $this->HTTP->getData('contact/saveemail.html', $data);
  135. if(strpos($response, 'has been'))
  136. {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * List all domains in the cPanel account
  143. *
  144. * Returns a numerically-indexed array on success or false on failure.
  145. * @return array
  146. */
  147. function listDomains()
  148. {
  149. $domainList = array();
  150. preg_match_all('/<option value="([^"]*)/', $this->HTTP->getData('mail/addpop2.html'), $domainList);
  151. if(count($domainList[1]) > 0)
  152. {
  153. return $domainList[1];
  154. }
  155. return false;
  156. }
  157. /**
  158. * List all POP3 email accounts
  159. *
  160. * Returns a numerically-indexed array on success or false on failure.
  161. * @return array
  162. */
  163. function listMailAccounts()
  164. {
  165. $accountList = array();
  166. preg_match_all('/\?user=([^"]*&)/', $this->HTTP->getData('mail/pops_noscript.html'), $accountList);
  167. if(count($accountList[1]) > 0)
  168. {
  169. return rtrim($accountList[1][0], '&');
  170. }
  171. return false;
  172. }
  173. /**
  174. * List MySQL database users
  175. *
  176. * Returns a numerically-indexed array on success. Returns an empty array if no users exist.
  177. * @return array
  178. */
  179. function listDBUsers()
  180. {
  181. $accountList = array();
  182. preg_match_all('/\?user=([^"]*)/', $this->HTTP->getData('sql/index.html'), $accountList);
  183. return $accountList[1];
  184. }
  185. /**
  186. * List MySQL databases
  187. *
  188. * Returns a numerically-indexed array on success. Returns an empty array if no databases exist.
  189. * @return array
  190. */
  191. function listDatabases()
  192. {
  193. $databaseList = array();
  194. preg_match_all('/deldbconfirm.html\?db=([^"]*)/', $this->HTTP->getData('sql/index.html'), $databaseList);
  195. return $databaseList[1];
  196. }
  197. /**
  198. * List FTP accounts
  199. *
  200. * Returns a numerically-indexed array on success or false on failure. This function does not include accounts listed as "Main Account".
  201. * @return array
  202. */
  203. function listFTPAccounts()
  204. {
  205. $accountList = Array();
  206. preg_match_all('/passwdftp.html\?acct=([^"]*)/', $this->HTTP->getData('ftp/accounts.html'), $accountList);
  207. return array_unique($accountList[1]);
  208. }
  209. /**
  210. * List parked domains
  211. *
  212. * Returns a numerically-indexed array on success. Returns an empty array if no domains are parked.
  213. * @return array
  214. */
  215. function listParked()
  216. {
  217. $domainList = array();
  218. preg_match_all('/<option value="([^"]*)/', $this->HTTP->getData('park/index.html'), $domainList);
  219. return $domainList[1];
  220. }
  221. /**
  222. * List addon domains
  223. *
  224. * Returns a numerically-indexed array of comma-delimited values on success. Returns an empty array if no addon domains exist.
  225. * @return array
  226. */
  227. function listAddons()
  228. {
  229. $domainList = array();
  230. $data = explode('Remove Addon', $this->HTTP->getData('addon/index.html'));
  231. preg_match_all('/<option value="(.*)">(.*)<\/option>/', $data[1], $domainList);
  232. return $domainList[0];
  233. }
  234. /**
  235. * List subdomains
  236. *
  237. * Returns a numerically-indexed array on success. Returns an empty array if no subdomains exist.
  238. * @return array
  239. */
  240. function listSubdomains()
  241. {
  242. $domainList = array();
  243. $domains = explode('</select>', $this->HTTP->getData('subdomain/index.html'));
  244. $domains = explode('</select>', $domains[2]);
  245. preg_match_all('/<option value="(.*)">(.*)<\/option>/', $domains[0], $domainList);
  246. return $domainList[2];
  247. }
  248. /**
  249. * List Apache redirects
  250. *
  251. * These may be permanent or temporary redirects (status codes 301 and 302). Returns a numerically-indexed array on success. Returns an empty array if no redirects exist.
  252. * @return array
  253. */
  254. function listRedirects()
  255. {
  256. $redirectList = array();
  257. preg_match_all('/<option value="\/([^"]*)/', $this->HTTP->getData('mime/redirect.html'), $redirectList);
  258. return $redirectList[1];
  259. }
  260. /**
  261. * Parse account information
  262. *
  263. * Returns General account information or General server information. Used internally by getFreeSpace(), getSpaceUsed(), etc.
  264. * @param string $key key to parse for
  265. * @param string $type type of value to return (int, float, or string)
  266. * @return string
  267. */
  268. function parseIndex($key, $type = 'string')
  269. {
  270. $value = array();
  271. preg_match('/' . $key . '<\/td>' . "\n" . ' <td class="index2">(.*)<\/td>/', $this->HTTP->getData('index.html'), $value);
  272. settype($value[1], $type);
  273. return $value[1];
  274. }
  275. /**
  276. * Get free disk space
  277. *
  278. * Returns the amount of disk space available in megabytes.
  279. * @return mixed
  280. */
  281. function getFreeSpace()
  282. {
  283. $freeSpace = $this->parseIndex('Disk space available', 'float');
  284. return ($freeSpace == 0) ? 'Unlimited' : floatval($freeSpace);
  285. }
  286. /**
  287. * Get used disk space
  288. *
  289. * Returns the amount of disk space used in megabytes.
  290. * @return float
  291. */
  292. function getSpaceUsed()
  293. {
  294. return $this->parseIndex('Disk Space Usage', 'float');
  295. }
  296. /**
  297. * Get MySQL space usage
  298. *
  299. * Returns the amount of disk space used by MySQL databases in megabytes.
  300. * @return float
  301. */
  302. function getMySQLSpaceUsed()
  303. {
  304. return $this->parseIndex('MySQL Disk Space', 'float');
  305. }
  306. /**
  307. * Get bandwidth usage
  308. *
  309. * Returns the amount of bandwidth used this month in megabytes.
  310. * @return float
  311. */
  312. function getBandwidthUsed()
  313. {
  314. return $this->parseIndex('Bandwidth \(this month\)', 'float');
  315. }
  316. /**
  317. * Get hosting package name
  318. * @return string
  319. */
  320. function getHostingPackage()
  321. {
  322. return $this->parseIndex('Hosting package');
  323. }
  324. /**
  325. * Get shared IP address
  326. * @return string
  327. */
  328. function getSharedIP()
  329. {
  330. return $this->parseIndex('Shared Ip Address');
  331. }
  332. /**
  333. * Creates an object to manipulate email account
  334. * @param string $address email address of account to manipulate
  335. * @return emailAccount
  336. */
  337. function openEmailAccount($address)
  338. {
  339. return new emailAccount($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $address);
  340. }
  341. /**
  342. * Creates an object to manipulate domain
  343. * @param string $domain domain to manipulate
  344. * @return Domain
  345. */
  346. function openDomain($domain)
  347. {
  348. return new Domain($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $domain);
  349. }
  350. /**
  351. * Creates an object to manipulate FTP account
  352. * @param string name of FTP account to manipulate
  353. * @return FTPAccount
  354. */
  355. function openFTPAccount($account)
  356. {
  357. return new FTPAccount($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $account);
  358. }
  359. /**
  360. * Creates an object to manipulate database
  361. * @param string $database name of MySQL database to manipulate
  362. * @return Database
  363. */
  364. function openDatabase($database)
  365. {
  366. return new Database($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $database);
  367. }
  368. /**
  369. * Creates an object to manipulate database user
  370. * @param string $user username of MySQL user to manipulate
  371. * @return databaseUser
  372. */
  373. function openDatabaseUser($user)
  374. {
  375. return new databaseUser($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $user);
  376. }
  377. /**
  378. * Creates an object to manipulate redirect
  379. * @param string $path server path to manipulate redirection on
  380. * @return Redirect
  381. */
  382. function openRedirect($path)
  383. {
  384. return new Redirect($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $path);
  385. }
  386. }
  387. /**
  388. * Functions to manipulate cPanel email accounts
  389. */
  390. class emailAccount
  391. {
  392. /**
  393. * @ignore
  394. */
  395. function emailAccount($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $address)
  396. {
  397. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  398. if(strpos($address, '@'))
  399. {
  400. list($this->email, $this->domain) = explode('@', $address);
  401. }
  402. else
  403. {
  404. list($this->email, $this->domain) = array($address, '');
  405. }
  406. }
  407. /**
  408. * Create email account in cPanel
  409. *
  410. * Returns true on success or false on failure.
  411. * @param string $password email account password
  412. * @param int $quota quota for email account in megabytes
  413. * @return bool
  414. */
  415. function create($password, $quota)
  416. {
  417. $data['email'] = $this->email;
  418. $data['domain'] = $this->domain;
  419. $data['password'] = $password;
  420. $data['quota'] = $quota;
  421. $response = $this->HTTP->getData('mail/doaddpop.html', $data);
  422. if(strpos($response, 'failure') || strpos($response, 'already exists'))
  423. {
  424. return false;
  425. }
  426. return true;
  427. }
  428. /**
  429. * Get space used by account
  430. *
  431. * Returns the amount of disk space used by email account in megabytes.
  432. * @return int
  433. */
  434. function getUsedSpace()
  435. {
  436. $usedSpace = array();
  437. preg_match('/' . $this->email . '@' . $this->domain . "<\\/font><\\/td>\n <td align=\"center\" valign=\"top\">([^&]*)/", $this->HTTP->getData('mail/pops.html?extras=disk'), $usedSpace);
  438. return $usedSpace[1];
  439. }
  440. /**
  441. * Get account storage quota
  442. *
  443. * Returns amount of disk space allowed for email account in megabytes.
  444. * @return int
  445. */
  446. function getQuota()
  447. {
  448. $quota = array();
  449. $data['email'] = $this->email;
  450. $data['domain'] = $this->domain;
  451. preg_match('/quota" value="([^"]*)/', $this->HTTP->getData('mail/editquota.html', $data), $quota);
  452. return ($quota[1] == 0) ? 'Unlimited' : intval($quota[1]);
  453. }
  454. /**
  455. * Modify account storage quota
  456. *
  457. * Returns true on success or false on failure.
  458. * @param int $quota quota for email account in megabytes
  459. * @return bool
  460. */
  461. function setQuota($quota)
  462. {
  463. $data['email'] = $this->email;
  464. $data['domain'] = $this->domain;
  465. $data['quota'] = $quota;
  466. $response = $this->HTTP->getData('mail/doeditquota.html', $data);
  467. if(strpos($response, 'success'))
  468. {
  469. return true;
  470. }
  471. return false;
  472. }
  473. /**
  474. * Change email account password
  475. *
  476. * Returns true on success or false on failure.
  477. * @param string $password email account password
  478. * @return bool
  479. */
  480. function setPassword($password)
  481. {
  482. $data['email'] = $this->email;
  483. $data['domain'] = $this->domain;
  484. $data['password'] = $password;
  485. $response = $this->HTTP->getData('mail/dopasswdpop.html', $data);
  486. if(strpos($response, 'success') && !strpos($response, 'failure'))
  487. {
  488. return true;
  489. }
  490. return false;
  491. }
  492. /**
  493. * List email forwarders
  494. *
  495. * Returns a numerically-indexed array of forwarders for the email account. Returns an empty array if there are no forwarders.
  496. * @return array
  497. */
  498. function listForwarders()
  499. {
  500. $forwarders = array();
  501. preg_match_all('/\?email=' . $this->email . '@' . $this->domain . '=([^"]*)/', $this->HTTP->getData('mail/fwds.html'), $forwarders);
  502. return $forwarders[1];
  503. }
  504. /**
  505. * Create email forwarder
  506. *
  507. * Returns true on success or false on failure.
  508. * @param string $forward forwarding address
  509. * @return bool
  510. */
  511. function addForwarder($forward)
  512. {
  513. $data['email'] = $this->email;
  514. $data['domain'] = $this->domain;
  515. $data['fwdemail'] = $forward;
  516. $data['fwdopt'] = 'fwd';
  517. $response = $this->HTTP->getData('mail/doaddfwd.html', $data);
  518. if(strpos($response, 'redirected'))
  519. {
  520. return true;
  521. }
  522. return false;
  523. }
  524. /**
  525. * Delete email forwarder
  526. *
  527. * Permanently removes the account's email forwarder and returns true.
  528. * @param string $forwarder forwarding address to delete
  529. * @return bool
  530. */
  531. function delForwarder($forwarder)
  532. {
  533. $data['email'] = $this->email . '@' . $this->domain . '=' . $forwarder;
  534. $this->HTTP->getData('mail/dodelfwd.html', $data);
  535. return true;
  536. }
  537. /**
  538. * Create email autoresponder
  539. *
  540. * Returns true on success or false on failure.
  541. * @param string $from from email address
  542. * @param string $subject email subject line
  543. * @param string $charset character set
  544. * @param bool $html true for HTML email
  545. * @param string $body body of email message
  546. * @return bool
  547. */
  548. function addAutoResponder($from, $subject, $charset, $html, $body)
  549. {
  550. $data['email'] = $this->email;
  551. $data['domain'] = $this->domain;
  552. $data['from'] = $from;
  553. $data['subject'] = $subject;
  554. $data['charset'] = $charset;
  555. if($html)
  556. {
  557. $data['html'] = $html;
  558. }
  559. $data['body'] = $body;
  560. $response = $this->HTTP->getData('mail/doaddars.html', $data);
  561. if(strpos($response, 'success') && !strpos($response, 'failure'))
  562. {
  563. return true;
  564. }
  565. return false;
  566. }
  567. /**
  568. * Delete email autoresponder
  569. *
  570. * Deletes autoresponder for email account if it exists, and returns true.
  571. * @return bool
  572. */
  573. function delAutoResponder()
  574. {
  575. $this->HTTP->getData('mail/dodelautores.html?email=' . $this->email . '@' . $this->domain);
  576. return true;
  577. }
  578. /**
  579. * Delete email account
  580. *
  581. * Permanenetly removes POP3 account. Returns true on success or false on failure.
  582. * @return bool
  583. */
  584. function delete()
  585. {
  586. $data['email'] = $this->email;
  587. $data['domain'] = $this->domain;
  588. $response = $this->HTTP->getData('mail/realdelpop.html', $data);
  589. if(strpos($response, 'success'))
  590. {
  591. return true;
  592. }
  593. return false;
  594. }
  595. }
  596. /**
  597. * Functions to manipulate domains in cPanel
  598. */
  599. class Domain
  600. {
  601. /**
  602. * @ignore
  603. */
  604. function Domain($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $domain)
  605. {
  606. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  607. $this->domain = $domain;
  608. }
  609. /**
  610. * Get default address
  611. *
  612. * Retrieves the default email address for the domain.
  613. * @return string
  614. */
  615. function getDefaultAddress()
  616. {
  617. $default = explode('<b>' . $this->domain . '</b>', $this->HTTP->getData('mail/def.html'));
  618. if($default[1])
  619. {
  620. $default = explode('<td>', $default[1]);
  621. $default = explode('</td>', $default[1]);
  622. return trim($default[0]);
  623. }
  624. }
  625. /**
  626. * Modify default address
  627. *
  628. * Changes the default email address for the domain. Returns true on success or false on failure.
  629. * @param string $adderss new default address
  630. * @return bool
  631. */
  632. function setDefaultAddress($address)
  633. {
  634. $data['domain'] = $this->domain;
  635. $data['forward'] = $address;
  636. $response = $this->HTTP->getData('mail/dosetdef.html', $data);
  637. if(strpos($response, 'is now'))
  638. {
  639. return true;
  640. }
  641. return false;
  642. }
  643. /**
  644. * Park domain
  645. *
  646. * Returns true on success or false on failure.
  647. * @return bool
  648. */
  649. function parkDomain()
  650. {
  651. $data['domain'] = $this->domain;
  652. $response = $this->HTTP->getData('park/doaddparked.html', $data);
  653. if(strpos($response, 'success') && !strpos($response, 'error'))
  654. {
  655. return true;
  656. }
  657. return false;
  658. }
  659. /**
  660. * Delete parked domain
  661. *
  662. * Returns true on success or false on failure.
  663. * @return bool
  664. */
  665. function unparkDomain()
  666. {
  667. $data['domain'] = $this->domain;
  668. $response = $this->HTTP->getData('park/dodelparked.html', $data);
  669. if(strpos($response, 'success') && !strpos($response, 'Error'))
  670. {
  671. return true;
  672. }
  673. return false;
  674. }
  675. /**
  676. * Create addon domain
  677. *
  678. * Returns true on success or false on failure.
  679. * @param string $user username or directory
  680. * @param string $pass password
  681. * @return bool
  682. */
  683. function addonDomain($user, $pass)
  684. {
  685. $data['domain'] = $this->domain;
  686. $data['user'] = $user;
  687. $data['pass'] = $pass;
  688. $response = $this->HTTP->getData('addon/doadddomain.html', $data);
  689. if(strpos($response, 'added') && !strpos($response, 'Error'))
  690. {
  691. return true;
  692. }
  693. return false;
  694. }
  695. /**
  696. * Delete addon domain
  697. *
  698. * Returns true on success or false on failure.
  699. * @return bool
  700. */
  701. function delAddonDomain()
  702. {
  703. $data['domain'] = $this->domain;
  704. $response = $this->HTTP->getData('addon/dodeldomain.html', $data);
  705. if(strpos($response, 'success') && !strpos($response, 'Error'))
  706. {
  707. return true;
  708. }
  709. return false;
  710. }
  711. /**
  712. * Create subdomain
  713. *
  714. * Returns true on success or false on failure.
  715. * @param string $subdomain name of subdomain to create
  716. * @return bool
  717. */
  718. function addSubdomain($subdomain)
  719. {
  720. $data['domain'] = $subdomain;
  721. $data['rootdomain'] = $this->domain;
  722. $response = $this->HTTP->getData('subdomain/doadddomain.html', $data);
  723. if(strpos($response, 'added') && !strpos($response, 'Error'))
  724. {
  725. return true;
  726. }
  727. return false;
  728. }
  729. /**
  730. * Get subdomain redirection
  731. *
  732. * Returns the URL a subdomain is redirected to.
  733. * @return string
  734. */
  735. function getSubdomainRedirect($subdomain)
  736. {
  737. $redirect = array();
  738. $data['domain'] = $subdomain . '_' . $this->domain;
  739. preg_match('/40 value="([^"]*)/', $this->HTTP->getData('subdomain/doredirectdomain.html', $data), $redirect);
  740. return $redirect[1];
  741. }
  742. /**
  743. * Redirect subdomain
  744. *
  745. * Redirects a subdomain of the current domain to another address.
  746. * @param string $subdomain name of subdomain
  747. * @param string $url url to redirect to
  748. * @return bool
  749. */
  750. function redirectSubdomain($subdomain, $url)
  751. {
  752. $data['domain'] = $subdomain . '_' . $this->domain;
  753. $data['url'] = $url;
  754. $response = $this->HTTP->getData('subdomain/saveredirect.html', $data);
  755. if(strpos($response, 'redirected') && !strpos($response, 'Disabled'))
  756. {
  757. return true;
  758. }
  759. return false;
  760. }
  761. /**
  762. * Remove subdomain redirection
  763. *
  764. * @param string $subdomain name of subdomain
  765. * @return bool
  766. */
  767. function delRedirectSubdomain($subdomain)
  768. {
  769. $data['domain'] = $subdomain . '_' . $this->domain;
  770. $response = $this->HTTP->getData('subdomain/donoredirect.html', $data);
  771. if(strpos($response, 'disabled'))
  772. {
  773. return true;
  774. }
  775. return false;
  776. }
  777. /**
  778. * Delete subdomain
  779. *
  780. * Returns true on success or false on failure.
  781. * @param string $subdomain name of subdomain to delete
  782. * @return bool
  783. */
  784. function delSubdomain($subdomain)
  785. {
  786. $data['domain'] = $subdomain . '_' . $this->domain;
  787. $response = $this->HTTP->getData('subdomain/dodeldomain.html', $data);
  788. if(strpos($response, 'Removed'))
  789. {
  790. return true;
  791. }
  792. return false;
  793. }
  794. }
  795. /**
  796. * Functions to manipulate cPanel FTP accounts
  797. */
  798. class FTPAccount
  799. {
  800. /**
  801. * Delete email autoresponder
  802. * @ignore
  803. */
  804. function FTPAccount($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $account)
  805. {
  806. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  807. $this->account = $account;
  808. }
  809. /**
  810. * Create FTP account
  811. *
  812. * Returns true on success or false on failure.
  813. * @param string $password account password
  814. * @param string $quota disk space quota in megabytes
  815. * @param string directory user's home directory
  816. * @return bool
  817. */
  818. function create($password, $quota, $directory)
  819. {
  820. $data['login'] = $this->account;
  821. $data['password'] = $password;
  822. $data['quota'] = $quota;
  823. $data['homedir'] = $directory;
  824. $response = $this->HTTP->getData('ftp/doaddftp.html', $data);
  825. if(strpos($response, 'failure') || strpos($response, 'Fatal') || !strpos($response, 'Added'))
  826. {
  827. return false;
  828. }
  829. return true;
  830. }
  831. /**
  832. * Get used space
  833. *
  834. * Returns the amount of disk space used by the FTP account.
  835. * @return int
  836. */
  837. function getUsedSpace()
  838. {
  839. $usedSpace = explode('<td>' . $this->account . '</td>', $this->HTTP->getData('ftp/accounts.html'));
  840. $usedSpace = explode('</td><td>', $usedSpace[1], 2);
  841. return floatval(substr($usedSpace[1], 0, strpos($usedSpace[1], '/')));
  842. }
  843. /**
  844. * Get storage quota
  845. *
  846. * Returns the storage quota of the FTP account in megabytes.
  847. * @return bool
  848. */
  849. function getQuota()
  850. {
  851. $quota = array();
  852. $data['acct'] = $this->account;
  853. preg_match('/"quota" value="([^"]*)/', $this->HTTP->getData('ftp/editquota.html', $data), $quota);
  854. return ($quota[1] == 0) ? 'Unlimited' : intval($quota[1]);
  855. }
  856. /**
  857. * Set storage quota
  858. *
  859. * Modifies the maximum disk space allowed for the FTP account. Returns true on success or false on failure.
  860. * @param int $quota new quota in megabytes
  861. * @return bool
  862. */
  863. function setQuota($quota)
  864. {
  865. $data['acct'] = $this->account;
  866. $data['quota'] = $quota;
  867. $response = $this->HTTP->getData('ftp/doeditquota.html', $data);
  868. if(strpos($response, 'success'))
  869. {
  870. return true;
  871. }
  872. return false;
  873. }
  874. /**
  875. * Change password
  876. *
  877. * Changes the FTP account password and returns true on success or false on failure.
  878. * @param string $password new password
  879. * @return bool
  880. */
  881. function setPassword($password)
  882. {
  883. $data['acct'] = $this->account;
  884. $data['password'] = $password;
  885. $response = $this->HTTP->getData('ftp/dopasswdftp.html', $data);
  886. if(strpos($response, 'Changed'))
  887. {
  888. return true;
  889. }
  890. return false;
  891. }
  892. /**
  893. * Delete FTP account
  894. *
  895. * Permanently removes the FTP account and returns true on success or false on failure.
  896. * @return bool
  897. */
  898. function delete()
  899. {
  900. $data['login'] = $this->account;
  901. $response = $this->HTTP->getData('ftp/realdodelftp.html', $data);
  902. if(strpos($response, 'deleted'))
  903. {
  904. return true;
  905. }
  906. return false;
  907. }
  908. }
  909. /**
  910. * Functions to manipulate MySQL databases in cPanel
  911. */
  912. class Database
  913. {
  914. /**
  915. * @ignore
  916. */
  917. function Database($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $database)
  918. {
  919. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  920. $this->database = $database;
  921. }
  922. /**
  923. * Create database
  924. *
  925. * Creates a MySQL database with the specified name. Returns true on success or false on failure.
  926. * @return bool
  927. */
  928. function create()
  929. {
  930. $data['db'] = $this->database;
  931. $response = $this->HTTP->getData('sql/addb.html', $data);
  932. if(strpos($response, 'Added'))
  933. {
  934. return true;
  935. }
  936. return false;
  937. }
  938. /**
  939. * Add user to database
  940. *
  941. * Gives the specified user all permissions on the database and returns true on success or false on failure.
  942. * @param string $user MySQL username to add to database
  943. * @return bool
  944. */
  945. function addUser($user)
  946. {
  947. $data['user'] = $user;
  948. $data['db'] = $this->database;
  949. $data['ALL'] = 'ALL';
  950. $response = $this->HTTP->getData('sql/addusertodb.html', $data);
  951. if(strpos($response, 'Added'))
  952. {
  953. return true;
  954. }
  955. return false;
  956. }
  957. /**
  958. * Delete user from database
  959. *
  960. * Removes the user's permissions from this database.
  961. * @param string $user MySQL username
  962. * @return bool
  963. */
  964. function delUser($user)
  965. {
  966. $data['user'] = $user;
  967. $data['db'] = $this->database;
  968. $response = $this->HTTP->getData('sql/deluserfromdb.html', $data);
  969. if(strpos($response, 'Deleted'))
  970. {
  971. return true;
  972. }
  973. return false;
  974. }
  975. /**
  976. * Delete database
  977. *
  978. * Permanently drops a MySQL database and returns true on success or false on failure.
  979. * @return bool
  980. */
  981. function delete()
  982. {
  983. $data['db'] = $this->database;
  984. $response = $this->HTTP->getData('sql/deldb.html', $data);
  985. if(strpos($response, 'dropped'))
  986. {
  987. return true;
  988. }
  989. return false;
  990. }
  991. }
  992. /**
  993. * Functions to manipulate MySQL database users in cPanel
  994. */
  995. class databaseUser
  996. {
  997. /**
  998. * @ignore
  999. */
  1000. function databaseUser($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $user)
  1001. {
  1002. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  1003. $this->user = $user;
  1004. }
  1005. /**
  1006. * Create database user
  1007. *
  1008. * Creates a MySQL user and returns true on success or false on failure.
  1009. * @param string $password MySQL password
  1010. * @return bool
  1011. */
  1012. function create($password)
  1013. {
  1014. $data['user'] = $this->user;
  1015. $data['pass'] = $password;
  1016. $response = $this->HTTP->getData('sql/adduser.html', $data);
  1017. if(strpos($response, 'Added'))
  1018. {
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. /**
  1024. * Delete database user
  1025. *
  1026. * Permenently deletes the MySQL user.
  1027. * @return bool
  1028. */
  1029. function delete()
  1030. {
  1031. $data['user'] = $this->user;
  1032. $response = $this->HTTP->getData('sql/deluser.html', $data);
  1033. if(strpos($response, 'Removed'))
  1034. {
  1035. return true;
  1036. }
  1037. return false;
  1038. }
  1039. }
  1040. /**
  1041. * Functions to manipulate URL redirection in cPanel
  1042. */
  1043. class Redirect
  1044. {
  1045. /**
  1046. * Delete email autoresponder
  1047. * @ignore
  1048. */
  1049. function Redirect($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $path)
  1050. {
  1051. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  1052. $this->path = '/' . $path;
  1053. }
  1054. /**
  1055. * Create redirect
  1056. *
  1057. * Creates a 301 or 302 redirect and returns true on success or false on failure.
  1058. * @param string $url URL to redirect to
  1059. * @param string $type 'permanent' or 'temp'
  1060. * @return bool
  1061. */
  1062. function create($url, $type = 'permanent')
  1063. {
  1064. $data['path'] = $this->path;
  1065. $data['url'] = $url;
  1066. $data['type'] = $type;
  1067. $response = $this->HTTP->getData('mime/addredirect.html', $data);
  1068. if(strpos($response, 'Added'))
  1069. {
  1070. return true;
  1071. }
  1072. return false;
  1073. }
  1074. /**
  1075. * Get redirect URL
  1076. *
  1077. * Get the path a URL is redirected to
  1078. * @return string
  1079. */
  1080. function getRedirectURL()
  1081. {
  1082. $url = array();
  1083. preg_match('%' . $this->path . '</td><td>([^<]*)%', $this->HTTP->getData('mime/redirect.html'), $url);
  1084. return $url[1];
  1085. }
  1086. /**
  1087. * Delete redirect
  1088. *
  1089. * Permanently removes the redirect and returns true on success or false on failure.
  1090. * @return bool
  1091. */
  1092. function delete()
  1093. {
  1094. $data['path'] = '/' . $this->path;
  1095. $response = $this->HTTP->getData('mime/delredirect.html', $data);
  1096. if(strpos($response, 'Removed'))
  1097. {
  1098. return true;
  1099. }
  1100. return false;
  1101. }
  1102. }
  1103. ?>